This page last changed on Oct 13, 2009 by michael.
Client Configuration
The RestClient configuration is performed by using the ClientConfig class. An instance of the configuration class is passed to the constructor of the RestClient when constructing a new RestClient.
The following options can be configured in the RestClient:
- Custom providers via JAX-RS Application
- Connect and read timeouts
Handler Configuration
The following example demonstrates how to register a custom handler.
1 ClientConfig config = new ClientConfig();
// Create new JAX-RS Application
2 config.handlers(new DummyHandler());
// create the rest client instance
3 RestClient client = new RestClient(config);
// create the resource instance to interact with
4 Resource resource = client.resource("http://services.com/HelloWorld");
// perform a GET on the resource
// the resource will be returned as plain text
5 String response = resource.accept("text/plain").get(String.class);
Explanation
First, a new instance of a ClientConfig is created as it appears in line 1. Then the new handler is added to the handlers chain by invoking the handlers() method on the ClientConfig instance as it appears in line 2. Finally, a new instance of a RestClient is created with this configuration as it appears in line 3.
Custom Provider Configuration
The following example demonstrates how to register a custom entity provider.
1 ClientConfig config = new ClientConfig();
// Create new JAX-RS Application
2 Application app = new Application() {
@Override
public Set<Class<?>> getClasses() {
HashSet<Class<?>> set = new HashSet<Class<?>>();
set.add(FooProvider.class);
return set;}};
3 conf.applications(app);
// create the rest client instance
4 RestClient client = new RestClient(config);
// create the resource instance to interact with
5 Resource resource = client.resource("http://services.com/HelloWorld");
// perform a GET on the resource. the resource will be returned as plain text
6 String response = resource.accept("text/plain").get(String.class);
Explanation
First, a new instance of ClientConfig is created as it appears in line 1. Then a new anonymous Application is instantiated and set on the ClientConfig as it appears in line 2 and 3. Finally, a new instance of a RestClient is created with this configuration as it appears in line 4.
|